package com.swu.pad.util;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.swu.pad.comment.dao.ConnectDatabase;
import com.swu.pad.util.EPLiteClient.EPLiteClient;

public class CommentTrans {
     public static List tranComment(){
    	 
    	 EPLiteClient epLiteClient = new EPLiteClient("http://localhost:8081/", "e56b0cd06c86da040c24aeb3882cda37af9f7438a83d42d3aec923ca40219a52");
    	// 2 接收给属性赋值
 		Connection conn = null;// 连接数据库
 		PreparedStatement pstmt = null;
 		ResultSet rs=null;
 		List data=new ArrayList();
 		try {
			// 插入comments语句
			String selectSql = "select id FROM assignmentpad";
			// 获得与数据库的连接
			conn = ConnectDatabase.getConnection();
			pstmt = (PreparedStatement) conn.prepareStatement(selectSql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				String padId=rs.getString(1);
			    Map result=epLiteClient.getCommentByPadId(padId);
			    Map map=(Map) result.get("comments");
			    Set commentId=map.keySet();
				 Iterator it =commentId.iterator();
				 while(it.hasNext()){				 
					 String commentID=it.next().toString();
					 if(commentID.length()>0){
					 Map mapSon=(Map) map.get(commentID);	
					 String text=mapSon.get("text").toString();
					 data.add(commentID+":"+text);
					 }
					
				 }
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("向数据库插入fbcomments发生异常！");
		}finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(pstmt!=null){
				try {
					pstmt.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
 		return data;
     }
     
     
     /**
      * 更新评论内容
      * @param commentId
      * @param text
      */
     public static void updateComment(String commentId,String text){
    	Connection conn_ = null;// 连接数据库
  		PreparedStatement pstmt_ = null;
  		ResultSet rs_=null;
  		try {
 			// 插入comments语句
 			String updateSql = "update fbcomment set comment_text =? where comment_id=? and feedback_type='0' ";
 			// 获得与数据库的连接
 			conn_ = ConnectDatabase.getConnection();
 			pstmt_ = (PreparedStatement) conn_.prepareStatement(updateSql);
 			pstmt_.setString(1,text);
 			pstmt_.setString(2,commentId);
 			pstmt_.executeUpdate();// 执行语句update
 			
 		} catch (Exception e) {
 			e.printStackTrace();
 			System.out.println("向数据库更新fbcomments发生异常！");
 		}finally{
 			if(rs_!=null){
 				try {
 					rs_.close();
 				} catch (SQLException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}
 			}
 			if(pstmt_!=null){
 				try {
 					pstmt_.close();
 				} catch (SQLException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}
 			}
 			if(conn_!=null){
 				try {
 					conn_.close();
 				} catch (SQLException e) {
 					// TODO Auto-generated catch block
 					e.printStackTrace();
 				}
 			}
 		}
     }
     
}
